#!/usr/bin/node

const SerialPort = require('serialport');

function usage() {
    console.error("Usage:");
    console.error('node terminal.js path');
    console.error('');
    console.error('Arguments:');
    console.error('  path: serial device path, for example: /dev/ttyUSB0');
    console.error('');
    
    process.exit();
}

if(process.argv.length <= 2) {
    usage();
}

var serialPath = process.argv[2];
var serialOptions = {
    autoOpen: false,
    baudRate: 123456
}

var port = new SerialPort(serialPath, serialOptions);
port.open(port_on_open);

function port_on_open(err) {
    if(err) {
        console.error(err.message);
        process.exit();
    }
    
    var ain = 0;
    var ain_hi_valid = false;
    
    port.on('error', function (err) {
        console.error(err.message);
        process.exit(1);
    });
    
    port.on('data', function (data) {
        console.log(data);
        console.log(data.toString());
    });
    
    process.on('SIGINT', function () {
        process.exit();
    });
    
    port.flush();
}

process.stdin.on('data', function (data) {
    port.write(data);
});

